package com.nanhai.buddhist.college.module.study.controller.admin.subjectconfig;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;
import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.success;

import com.nanhai.buddhist.college.framework.excel.core.util.ExcelUtils;

import com.nanhai.buddhist.college.framework.operatelog.core.annotations.OperateLog;
import static com.nanhai.buddhist.college.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.nanhai.buddhist.college.module.study.controller.admin.subjectconfig.vo.*;
import com.nanhai.buddhist.college.module.study.dal.dataobject.subjectconfig.StudySubjectConfigDO;
import com.nanhai.buddhist.college.module.study.convert.subjectconfig.StudySubjectConfigConvert;
import com.nanhai.buddhist.college.module.study.service.subjectconfig.StudySubjectConfigService;

@Api(tags = "管理后台 - 课程管理-配置")
@RestController
@RequestMapping("/study/subject-config")
@Validated
public class StudySubjectConfigController {

    @Resource
    private StudySubjectConfigService subjectConfigService;

    @PostMapping("/create")
    @ApiOperation("创建课程管理-配置")
   // @PreAuthorize("@ss.hasPermission('study:subject-config:create')")
    public CommonResult<Long> createSubjectConfig(@Valid @RequestBody StudySubjectConfigCreateReqVO createReqVO) {
        return success(subjectConfigService.createSubjectConfig(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新课程管理-配置, 课程配置保存也使用该方法。")
   // @PreAuthorize("@ss.hasPermission('study:subject-config:update')")
    public CommonResult<Boolean> updateSubjectConfig(@Valid @RequestBody StudySubjectConfigUpdateReqVO updateReqVO) {
        subjectConfigService.updateSubjectConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除课程管理-配置")
   // @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('study:subject-config:delete')")
    public CommonResult<Boolean> deleteSubjectConfig(@RequestParam("id") Long id) {
        subjectConfigService.deleteSubjectConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得课程管理-配置")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    //@PreAuthorize("@ss.hasPermission('study:subject-config:query')")
    public CommonResult<StudySubjectConfigRespVO> getSubjectConfig(@RequestParam("id") Long id) {
        StudySubjectConfigDO subjectConfig = subjectConfigService.getSubjectConfig(id);
        return success(StudySubjectConfigConvert.INSTANCE.convert(subjectConfig));
    }

    @GetMapping("/getBySubjectId")
    @ApiOperation("获得课程管理-配置  根据课程来获取配置信息")
    @ApiImplicitParam(name = "id", value = "课程ID", required = true, example = "1024", dataTypeClass = String.class)
//    @PreAuthorize("@ss.hasPermission('study:subject-config:query')")
    public CommonResult<StudySubjectConfigRespVO> getBySubjectId(@RequestParam("id") String id) {
        StudySubjectConfigDO subjectConfig = subjectConfigService.getSubjectConfigBySubjectId(id);
        if(Objects.isNull(subjectConfig)){
            subjectConfig = new StudySubjectConfigDO();
        }
        return success(StudySubjectConfigConvert.INSTANCE.convert(subjectConfig));
    }

    @GetMapping("/list")
    @ApiOperation("获得课程管理-配置列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    //@PreAuthorize("@ss.hasPermission('study:subject-config:query')")
    public CommonResult<List<StudySubjectConfigRespVO>> getSubjectConfigList(@RequestParam("ids") Collection<Long> ids) {
        List<StudySubjectConfigDO> list = subjectConfigService.getSubjectConfigList(ids);
        return success(StudySubjectConfigConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得课程管理-配置分页")
   // @PreAuthorize("@ss.hasPermission('study:subject-config:query')")
    public CommonResult<PageResult<StudySubjectConfigRespVO>> getSubjectConfigPage(@Valid StudySubjectConfigPageReqVO pageVO) {
        PageResult<StudySubjectConfigDO> pageResult = subjectConfigService.getSubjectConfigPage(pageVO);
        return success(StudySubjectConfigConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出课程管理-配置 Excel")
    //@PreAuthorize("@ss.hasPermission('study:subject-config:export')")
    @OperateLog(type = EXPORT)
    public void exportSubjectConfigExcel(@Valid StudySubjectConfigExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<StudySubjectConfigDO> list = subjectConfigService.getSubjectConfigList(exportReqVO);
        // 导出 Excel
        List<StudySubjectConfigExcelVO> datas = StudySubjectConfigConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "课程管理-配置.xls", "数据", StudySubjectConfigExcelVO.class, datas);
    }

}
